
/**
 * @author Daniel Weber
 * Die Engine des Spiels.
 * Führt die Hauptschleife in Game.java aus und bereitet das Spiel selbst vor.
 */
import java.io.IOException;
import javax.microedition.lcdui.game.GameCanvas;
import javax.microedition.lcdui.Graphics;

public class ActionCanvas extends GameCanvas implements Runnable {

    ProjectGravity midlet;
    static final int MAX_GAME_SPEED = 33;
    private boolean startPreLoader;
    static boolean bUp = false,
            bDown = false,
            bLeft = false,
            bRight = false,
            bFire = false,
            bGameA = false,
            bGameB = false,
            bGameC = false,
            bGameD = false,
            bAnyKey = false;

    ActionCanvas(ProjectGravity midlet) {
        super(false);
        this.midlet = midlet;
        setFullScreenMode(true);
        //Bildschirmmaße werden gespeichert
        system.w = this.getWidth();
        system.h = this.getHeight();

        new Thread(this).start();
    }

    public void run() {
        try {
            while (true) {
                long startTime = System.currentTimeMillis();
                updateGameScreen(getGraphics());
                Thread.yield();
                long executionTime = System.currentTimeMillis() - startTime;
                if (executionTime < MAX_GAME_SPEED) {
                    Thread.sleep(MAX_GAME_SPEED - (int) executionTime);
                    system.fps = 1000 / MAX_GAME_SPEED;
                } else {
                    system.fps = 1000 / (int) executionTime;
                }
            }
        } catch (Exception e) {
        }
    }

    public void updateGameScreen(Graphics g) throws IOException {
        //essentielle Daten werde geladen bevor es losgehen kann
        if (!system.preloaderReady) {
            g.drawString("Ladevorgang läuft...", 0, system.h / 2, system.anchor);
            if (!startPreLoader) {
                system.preloader();
                startPreLoader = true;
            }
        } else {
            //checkControlls();
            Game.loop(midlet, g);
        }
        //zeichnet alles auf den Bildschirm
        flushGraphics();

    }

    protected void keyPressed(int keyCode) {
        int key = getGameAction(keyCode);
        bAnyKey = true;
        switch (key) {
            case GAME_A:
                bGameA = true;
                break;
            case GAME_B:
                bGameB = true;
                break;
            case GAME_C:
                bGameC = true;
                break;
            case GAME_D:
                bGameD = true;
                break;
            case UP:
                bUp = true;
                break;
            case DOWN:
                bDown = true;
                break;
            case LEFT:
                bLeft = true;
                break;
            case RIGHT:
                bRight = true;
                break;
            case FIRE:
                bFire = true;
                break;
        }

    }

    protected void keyReleased(int keyCode) {
        int key = getGameAction(keyCode);
        bAnyKey = false;
        switch (key) {
            case GAME_A:
                bGameA = false;
                break;
            case GAME_B:
                bGameB = false;
                break;
            case GAME_C:
                bGameC = false;
                break;
            case GAME_D:
                bGameD = false;
                break;
            case UP:
                bUp = false;
                break;
            case DOWN:
                bDown = false;
                break;
            case LEFT:
                bLeft = false;
                break;
            case RIGHT:
                bRight = false;
                break;
            case FIRE:
                bFire = false;
                break;
        }
    }

    public static void clearButtons() {
        bUp = false;
        bDown = false;
        bLeft = false;
        bRight = false;
        bFire = false;
        bGameA = false;
        bGameB = false;
        bGameC = false;
        bGameD = false;
        bAnyKey = false;
    }
}
